package com.easy.base.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.easy.auth.domain.Command;
import com.easy.auth.domain.Userinfo;
import com.easy.base.domain.EObject;
import com.easy.common.util.SpringUtil;
import com.easy.system.service.LovService;

public class ESession {
	public static final String SESSION_USER = "easyUser";
	public static final String SESSION_LOCAL = "easyLocal";
	public static final String SESSION_COMMAND = "easyCommand";
	
	private Userinfo userinfo;
	private String local  = "chinese";;
	private Map<String, Command> commandMap;
	
	private LovService lovService;
	
	private ESession() {
		
	}
	
	@SuppressWarnings("unchecked")
	public static ESession create(HttpSession httpSession) {
		ESession eSession = new ESession();
		String local = null;
		Map<String, Command> commandMap = null;
		
		if (httpSession != null) {
			eSession.userinfo = (Userinfo) httpSession.getAttribute(SESSION_USER);
			local = (String)httpSession.getAttribute(SESSION_LOCAL);
			
			commandMap = (Map<String, Command>)httpSession.getAttribute(SESSION_COMMAND);
			
			if (commandMap == null) {
				commandMap = new HashMap<String, Command>();
				httpSession.setAttribute(SESSION_COMMAND, commandMap);
			}
		}
		
		 
		
		eSession.local = local;
		//eSession.commandService = SpringUtil.getBean("commandServiceImpl");
		eSession.lovService = SpringUtil.getBean("lovServiceImpl");
		eSession.commandMap = commandMap;
		return eSession;
	}
	
	public <T extends EObject> boolean doCommand(String key, Class<T> clazz) {
		return true;
	}
	
	public Userinfo getUserinfo() {
		return this.userinfo;
	}
	
	public String getUserid() {
		if (this.userinfo != null) {
			return this.userinfo.getUserid();
		}
		
		return "";
	}
	
	public String getUsername() {
		if (this.userinfo != null) {
			return this.userinfo.getOname();
		}
		
		return "";
	}
	
	public String local(String key) {
		return local(key, key);
	}
	
	public String local(String key, String defauleValue) {
		
		//return this.nativeService.local(key, this.local, defauleValue);
		return defauleValue;
	}
	
	public String local(EObject eo) {
		String key = eo.getClass().getName() + "_" + eo.getOid();
		
		return local(key, eo.getOname());
	}
	
	public String local(Class<? extends EObject> clazz, String attr) {
		return local(clazz, attr, clazz.getName() + ".attribute." + attr);
	}
	
	public String local(Class<? extends EObject> clazz, String attr, String defauleValue) {
		String key = clazz.getName() + ".attribute." + attr;
		
		return local(key, defauleValue);
	}
	
//	public String localLov(String type, String key) {
//		String localKey = type + "." + key;
//		String defaultValue = lovService.findLovTypeName("boolean", key);
//		
//		return local(localKey, defaultValue);
//	}
	
	public String localItemType(String oid, String value) {
		String localKey = "ItemType." + oid;
		
		return local(localKey, value);
	}
	
	public String localItemClassify(String oid, String value) {
		String localKey = "ItemClassify." + oid;
		
		return local(localKey, value);
	}
	
	public String localItemForm(String oid, String value) {
		String localKey = "ItemForm." + oid;
		
		return local(localKey, value);
	}
	
	public String localFormAttribute(String oid, String value) {
		String localKey = "FormAttribute." + oid;
		
		return local(localKey, value);
	}
	
	public String localFormAttributeGroup(String oid, String value) {
		String localKey = "FormAttributeGroup." + oid;
		
		return local(localKey, value);
	}
	public String localFormRelation(String oid, String value) {
		String localKey = "FormRelation." + oid;
		
		return local(localKey, value);
	}
	public String localLog(String oid, String value) {
		String localKey = "Log." + oid;
		
		return local(localKey, value);
	}
	public boolean hasSave(String clazz) {
		//return hasCommand(clazz, Command.SAVE);
		return true;
	}
	
	public boolean hasSaveAs(String clazz) {
		//return hasCommand(clazz, Command.SAVE_AS);
		return true;
	}
	
	public boolean hasEdit(String clazz) {
		//return hasCommand(clazz, Command.EDIT);
		return true;
	}
	public boolean hasDowload() {

		
		return true;
	}
//	
//	public boolean hasExport(String clazz) {
//		return hasCommand(clazz, Command.EXPORT);
//	}
//	
	public boolean hasImport(String clazz) {
		return true;//return hasCommand(clazz, Command.IMPORT);
	}
//	
//	public boolean hasRelease(String clazz) {
//		return hasCommand(clazz, Command.RELEASE);
//	}
//	
	public boolean hasRemove(String clazz) {
		//return hasCommand(clazz, Command.REMOVE);
		return true;
	}
//	
//	public boolean hasUnRelease(String clazz) {
//		return hasCommand(clazz, Command.UN_RELEASE);
//	}
//	
	public boolean hasView(String clazz) {
		return true;
		//return hasCommand(clazz, Command.VIEW);
	}
	
//	public boolean hasCommand(String clazz, int code) {
//		Command command = getCommand(clazz);
//		
//		if (command == null) {
//			return false;
//		}
//		
//		return command.hasCommand(code);
//	}
	
//	private Command getCommand(String clazz) {
//		Userinfo userinfo = getUserinfo();
//		if (userinfo == null) {
//			return null;
//		}
//		
//		String userOid = getUserinfo().getOid();
//		String objectKey = clazz.getName();
//		
//		Command command = null;
//		
//		if (!commandMap.containsKey(objectKey)) {
//			command = commandService.generateCommmand(userOid, objectKey);
//			commandMap.put(objectKey, command);
//		} 
//		return commandMap.get(objectKey);
//	}
	
}
